package com.pet.shop.service.pet;

import com.github.pagehelper.Page;
import com.pet.exceptions.BusinessException;
import com.pet.shop.dto.PageDto;
import com.pet.shop.entity.shop.*;
import com.pet.shop.entity.user.UserInfo;
import com.pet.shop.mappers.shop.*;
import com.pet.shop.mappers.user.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.Relation;
import java.util.List;
import java.util.Map;

@Service
public class PetInfoServiceImpl implements IPetInfoService {

    @Autowired
    private PetInfoMapper petInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private PetInjectionMapper petInjectionMapper;
    @Autowired
    private PetShowerMapper petShowerMapper;
    @Autowired
    private PetRelayMapper petRelayMapper;
    @Autowired
    private PetDisinsectionMapper petDisinsectionMapper;

    @Override
    public int deleteEntity(Long id) {
        return petInfoMapper.delete(id);
    }

    @Override
    public int deleteEntityList(List<Long> idList) {
        if(idList == null || idList.isEmpty()) return 0;
        int i = 0;
        for (Long id : idList) {
            i += petInfoMapper.delete(id);
        }
        return i;
    }

    @Override
    public PetInfo insertEntity(PetInfo record) {
        if(record == null || record.getUserId() == null){
            throw new BusinessException("宠物信息必须包含主人信息");
        }
        petInfoMapper.insert(record);
        return record;
    }

    @Override
    public PetInfo getEntity(Long id) {
        PetInfo pet = petInfoMapper.get(id);
        if(pet == null) return null;
        UserInfo userInfo = userInfoMapper.get(pet.getUserId());
        if(userInfo != null) pet.setUserName(userInfo.getNickname());
        return pet;
    }

    @Override
    public int updateEntity(PetInfo record) {
        return petInfoMapper.update(record);
    }

    @Override
    public PageDto<PetInfo> queryPage(PageDto<PetInfo> pageDto) {
        Page<PetInfo> page = petInfoMapper.queryPage(pageDto);
        pageDto.setPageResult(page);
        return pageDto;
    }

    @Override
    public List<PetInfo> queryForPetList(Map<String,Object> map){
        return petInfoMapper.queryPetInInfoListByParams(map);
    }

    @Override
    public List<PetShower> queryForShowerList(Long petId){
        return petShowerMapper.queryByPetId(petId);
    }

    @Override
    public List<PetInjection> queryForInjectionList(Long petId){
        return petInjectionMapper.queryByPetId(petId);
    }

    @Override
    public List<PetDisinsection> queryForPetDisinsecList(Long petId){
        return petDisinsectionMapper.queryByPetId(petId);
    }

    @Override
    public List<PetRelay> queryForPetRelationList(Long petId){
        return petRelayMapper.queryByPetId(petId);
    }

    @Override
    public PetRelay insertRelayInfo(PetRelay petRelay){
        int relayId = petRelayMapper.insert(petRelay);
        petRelay.setId((long)relayId);
        return petRelay;
    }

    @Override
    public List<PetRelay> queryForPetRelationListByUserId(Long userId){
        return petRelayMapper.queryByUserId(userId);
    }
}
